package com.example.diancai.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.diancai.consts.RedisKeyConst;
import com.example.diancai.entity.GameRecord;
import com.example.diancai.entity.WxUser;
import com.example.diancai.enums.Game2048LevelEnum;
import com.example.diancai.mapper.GameRecordMapper;
import com.example.diancai.util.RedisUtil;
import com.example.diancai.websocket.dto.ClientInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GameRecordService extends ServiceImpl<GameRecordMapper, GameRecord> {

    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private RedisUtil redisUtil;

    public void saveRecord(GameRecord gameRecord, ClientInfoDTO clientInfoDTO1, ClientInfoDTO clientInfoDTO2) {
        save(gameRecord);
        Integer win = gameRecord.getWin();

        ClientInfoDTO winClient;
        //获取赢家的用户信息
        WxUser winWxUser;

        ClientInfoDTO failClient;
        //获取赢家的用户信息
        WxUser failWxUser;
        if (win == 1) {
            winWxUser = wxUserService.getById(gameRecord.getWxUserOneId());
            winClient = clientInfoDTO1;
            failWxUser = wxUserService.getById(gameRecord.getWxUserTwoId());
            failClient = clientInfoDTO2;
        } else {
            winWxUser = wxUserService.getById(gameRecord.getWxUserTwoId());
            winClient = clientInfoDTO2;
            failWxUser = wxUserService.getById(gameRecord.getWxUserOneId());
            failClient = clientInfoDTO1;
        }

        if (!winClient.isAI()) {
            Integer star = winWxUser.getStar();
            if (star == null) {
                star = 0;
            }
            star = star + 1;
            Game2048LevelEnum game2048Level = winWxUser.getGame2048Level();
            if (star > game2048Level.getStar()) {
                game2048Level = game2048Level.nextLevel();
                star = 0;
            }

            winWxUser.setGame2048Level(game2048Level);
            winWxUser.setStar(star);
            wxUserService.updateById(winWxUser);
            // 连接修改
            winClient.getCurrentPlayer().setGame2048Level(game2048Level);
            winClient.getCurrentPlayer().setStar(star);
            // 缓存修改
            String token = redisUtil.getObj(RedisKeyConst.AUTH + winClient.getCurrentPlayer().getWxId(), String.class);
            redisUtil.setObject(RedisKeyConst.AUTH + token, winClient.getCurrentPlayer());
        }


        // 输家
        if (!failClient.isAI()) {
            Integer failStar = failWxUser.getStar();
            if (failStar == null) {
                failStar = 0;
            }
            failStar = failStar - 1;
            Game2048LevelEnum failGame2048Level = failWxUser.getGame2048Level();
            if (failStar < 0) {
                failStar = 0;
                if (failGame2048Level.ordinal() != 0) {
                    failGame2048Level = failGame2048Level.beforeLevel();
                    failStar = failGame2048Level.getStar();
                }

            }

            failWxUser.setGame2048Level(failGame2048Level);
            failWxUser.setStar(failStar);
            wxUserService.updateById(failWxUser);
            // 连接修改
            failClient.getCurrentPlayer().setGame2048Level(failGame2048Level);
            failClient.getCurrentPlayer().setStar(failStar);
            // 缓存修改
            String failToken = redisUtil.getObj(RedisKeyConst.AUTH + failClient.getCurrentPlayer().getWxId(), String.class);
            redisUtil.setObject(RedisKeyConst.AUTH + failToken, failClient.getCurrentPlayer());
        }


    }
}
